package test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

/**
 * Created by MtRider on 2021/10/30 11:28
 */
public class ThreeSums {
    public static void main(String[] args) {
        //[-1,0,1,2,-1,-4]
        int nums[]={-1,0,1,2,-1,-4};
        System.out.println(threeSum(nums));
    }

    public static List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> list = new ArrayList();
        if (nums.length < 3) {
            return list;
        }
        Arrays.sort(nums);
        // i j k
        for (int i = 0; i < nums.length - 2; i++) {
            for (int j = i + 1; j < nums.length - 1; j++) {
                for (int k = j + 1; k < nums.length; k++) {
                    if (nums[i] + nums[j] + nums[k] == 0) {
                        int flag = 0;
                        List<Integer> integerList = new ArrayList<>();
                        integerList.add(nums[i]);
                        integerList.add(nums[j]);
                        integerList.add(nums[k]);
                        for (List<Integer> integers : list) {
                            if (checkDiffrent(integers, integerList)) {
                                flag = 1;
                                break;
                            }
                        }
                        if (flag == 0) {
                            list.add(integerList);
                        }
                    }
                }
            }
        }
        return list;
    }

    public static boolean checkDiffrent(List<Integer> list, List<Integer> list1) {
        return list.toString().equals(list1.toString());
    }
}
